//
//  YJWEssenceViewController.m
//  百思不得骑姐
//
//  Created by YJW on 16/10/10.
//  Copyright © 2016年 yijiang. All rights reserved.
//

#import "YJWEssenceViewController.h"
#import "YJWRecommendTagController.h"

#import "YJWAllViewController.h"
#import "YJWVideoViewController.h"
#import "YJWPicViewController.h"
#import "YJWWordViewController.h"
#import "YJWAudioViewController.h"

@interface YJWEssenceViewController ()<UIScrollViewDelegate>

/** 指示器   */
@property(nonatomic,strong) UIView *indicator;
/** 记录上一次按钮   */
@property(nonatomic,strong) UIButton *selectedBtn;
/** titleView   */
@property(nonatomic,strong) UIView *titleView;
/** 内容视图   */
@property(nonatomic,strong) UIScrollView *contentView;

@end

@implementation YJWEssenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置导航栏
    [self setUpNav];
    
    //设置子控制器
    [self setUpChildViews];
    
    //设置标题标签
    [self setUpTitleView];
    
    //添加内容视图
    [self setUpContentView];
}

/**
 * 设置子控制器
 */
-(void)setUpChildViews
{
    YJWWordViewController *word = [[YJWWordViewController alloc] init];
    word.title = @"段子";
    [self addChildViewController:word];
    
    YJWAllViewController *all = [[YJWAllViewController alloc] init];
    all.title = @"全部内容";
    [self addChildViewController:all];
    
    YJWVideoViewController *video = [[YJWVideoViewController alloc] init];
    video.title = @"视频";
    [self addChildViewController:video];
    
    YJWPicViewController *pic = [[YJWPicViewController alloc] init];
    pic.title = @"图片";
    [self addChildViewController:pic];
    
    YJWAudioViewController *audio = [[YJWAudioViewController alloc] init];
    audio.title = @"音频";
    [self addChildViewController:audio];
}

/**
 *  设置导航栏
 */
-(void)setUpNav
{
    //设置导航栏标题栏
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MainTitle"]];
    
    //设置导航栏的其它 item
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImg:@"MainTagSubIcon" highlightedImg:@"MainTagSubIconClick" target:self action:@selector(mainClick)];
    
    self.view.backgroundColor = YJWBgColor;

}

-(void)mainClick
{
    YJWRecommendTagController *rc = [[YJWRecommendTagController alloc] init];
    [self.navigationController pushViewController:rc animated:YES];
}

/**
 * 设置标题标签
 */
-(void)setUpTitleView
{
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = YJWRGBAColor(243, 243, 243, 0.8);
//    titleView.alpha = 0.7;  这样会将所有的子控件都设置为半透明
    titleView.width = self.view.width;
    titleView.height = 35;
    titleView.y = 64;
    [self.view addSubview:titleView];
    self.titleView = titleView;
    
    //设置标题标签指示器
    UIView *indicator = [[UIView alloc] init];
    indicator.backgroundColor = [UIColor redColor];
    indicator.height = 2;
    indicator.y = titleView.height - indicator.height;
    self.indicator = indicator;
    
    //设置标题标签
    float h = titleView.height;
    float w = titleView.width / self.childViewControllers.count;
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.height = h;
        btn.width = w;
        btn.x = w * i;
        //给 btn 绑定 tag
        btn.tag = i;
        [btn setTitle:self.childViewControllers[i].title forState:UIControlStateNormal];
        //        [btn layoutIfNeeded];//强制布局（强制更新子控件的 frame）
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        //让被选中的按钮不能再被点击
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [btn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:btn];
        
        //设置默认选中第一个标题
        if (i == 0) {
            btn.enabled = NO;
            self.selectedBtn = btn;
            
            [btn.titleLabel sizeToFit];
            self.indicator.width = btn.titleLabel.width;
            //            self.indicator.width = [titles[i] sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}].width;
            self.indicator.centerX = btn.centerX;
        }
    }
    
    [titleView addSubview:indicator];
}

/**
 * 点击相应的 title
 */
-(void)titleClick:(UIButton *)btn
{
    //取消之前被选中的按钮
    self.selectedBtn.enabled = YES;
    btn.enabled = NO;
    self.selectedBtn = btn;
    YJWFUNC;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.indicator.width = btn.titleLabel.width;
        self.indicator.centerX = btn.centerX;
    }];
    
    //点击标题，然后下方的内容进行滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = self.contentView.width * btn.tag;
    [UIView animateWithDuration:0.25 animations:^{
        [self.contentView setContentOffset:offset animated:YES];
    }];
}

/**
 * 添加内容视图
 */
-(void)setUpContentView
{
    //取消自动设置内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //添加内容视图
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.frame = self.view.bounds;
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    contentView.delegate = self;
    self.contentView = contentView;
    contentView.pagingEnabled = YES;
    [self.view insertSubview:contentView atIndex:0];
    
    //默认加载第一个控制器的 view  
    [self scrollViewDidEndScrollingAnimation:contentView];
}

#pragma mark - <UIScrollViewDelegate>
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    //取出子控制器
    UITableViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0;  //默认设置 view 的 y 从 20 开始
    vc.view.height = scrollView.height;
    
    [scrollView addSubview:vc.view];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //滑动内容时，让标题也滑动
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titleView.subviews[index]];

    [self scrollViewDidEndScrollingAnimation:scrollView];
}

@end
